package edu.gyc.hishiro.service.impl;

import com.alibaba.fastjson.support.odps.udf.CodecCheck;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import edu.gyc.hishiro.dao.SysPermissionDao;
import edu.gyc.hishiro.model.SysPermission;
import edu.gyc.hishiro.model.SysRole;
import edu.gyc.hishiro.dao.SysRoleDao;
import edu.gyc.hishiro.model.SysRolePermission;
import edu.gyc.hishiro.model.UserInfo;
import edu.gyc.hishiro.service.SysPermissionService;
import edu.gyc.hishiro.service.SysRolePermissionService;
import edu.gyc.hishiro.service.SysRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.gyc.hishiro.service.UserInfoService;
import edu.gyc.hishiro.vo.AllRoleUserVo;
import edu.gyc.hishiro.vo.PermissionVo;
import edu.gyc.hishiro.vo.RoleUser;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ls
 * @since 2021-01-04
 */
@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleDao, SysRole> implements SysRoleService {
    @Autowired
    SysRoleDao roleDao;

    @Autowired
    SysPermissionDao permissionDao;

    @Autowired
    SysPermissionService permissionService;

    @Override
    public List<PermissionVo> queryPermissionByUserId(Integer id) {
        List<PermissionVo> permissionVoList=null;
        Set<SysRole> roles = roleDao.findRolesByUserId(id);
        if (roles != null&&roles.size()>0) {
            Set<SysPermission> permissions = permissionDao.findPermissionsByRoleId(roles);
            List<SysPermission> permissionList = new ArrayList<>(permissions);

            List<Integer> acquireIds=new ArrayList<>();
            for (SysPermission permission : permissionList) {
                acquireIds.add(permission.getId());

            }
             permissionVoList = getPermissionVoList(acquireIds);
        }


        return permissionVoList;
    }

    private List<PermissionVo> getPermissionVoList(List<Integer> acquirePermissionIds){

        List<SysPermission> AllPermissions=permissionService.list();
        List<PermissionVo> permissionVos = new ArrayList<>();
        for (SysPermission permission : AllPermissions) {
            int n=permission.getId();
            PermissionVo permissionVo=new PermissionVo();
            BeanUtils.copyProperties(permission,permissionVo);

            if (acquirePermissionIds.contains(n)) {
                permissionVo.setAcquireStatus(true);
            }
            permissionVos.add(permissionVo);
        }

        permissionVos.sort(new Comparator<PermissionVo>() {
            @Override
            public int compare(PermissionVo o1, PermissionVo o2) {
                int n=o1.getId();
                int m=o2.getId();
                if(n>m)
                    return 1;
                else if (n < m) {
                    return -1;
                }
                else {
                    return 0;
                }
            }
        });
        return permissionVos;
    }

    @Override
    public List<PermissionVo> queryPermissionByRoleId(Integer id) {
        List<SysPermission> rolePermissionList = roleDao.findPermissionByRoleId(id);
        List<Integer> acquireIds=new ArrayList<>();
        for (SysPermission permission : rolePermissionList) {
            acquireIds.add(permission.getId());

        }
        return getPermissionVoList(acquireIds);
    }

    @Autowired
    SysRolePermissionService rolePermissionService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateRolePermissions(Integer roleId,List<Integer> ids) {
        if (ids.size() > 0) {
            rolePermissionService.remove(new LambdaUpdateWrapper<SysRolePermission>()
                    .eq(SysRolePermission::getRoleId,roleId));
//模拟异常，检测事务
//            int n=1,m=0;
//            n=n/0;

            List<SysRolePermission> rolePermissions = new ArrayList<>();
            for (Integer id : ids) {
                SysRolePermission rolePermission = new SysRolePermission();
                rolePermission.setRoleId(roleId);
                rolePermission.setPermissionId(id);
                rolePermissions.add(rolePermission);
            }

            rolePermissionService.saveBatch(rolePermissions);
        }

    }

    @Override
    public Set<RoleUser> getRoleUserList() {

        List<RoleUser> roleUsers=roleDao.getRoleUsers();
        Set<RoleUser> roleUserSet = new LinkedHashSet<>();
        for (RoleUser roleUser : roleUsers) {
            roleUserSet.add(roleUser);
        }

        for (RoleUser roleUser : roleUserSet) {
            roleUser.getNameSet().add(roleUser.getUsername());
            for (RoleUser t : roleUsers) {
                if (roleUser.getId() == t.getId()) {
                    if(!roleUser.getUsername().equals(t.getUsername())){

                        roleUser.getNameSet().add(t.getUsername());
                    }
                }
            }

            roleUser.setNames(roleUser.getNameSet().toString());

        }

        return roleUserSet;
    }
@Autowired
    UserInfoService userInfoService;
    @Override
    public AllRoleUserVo getAllRoleUserList() {
        List<SysRole> roles=this.list();
        List<UserInfo> userInfos=userInfoService.list();
        AllRoleUserVo allRoleUserVo=new AllRoleUserVo();
        allRoleUserVo.setRoles(roles);
        allRoleUserVo.setUserInfos(userInfos);
        return allRoleUserVo;
    }
}
